/**
 * 向量类
 */
export class Vector {
  private _x: number;
  private _y: number;
  constructor(x: number, y: number) {
    this._x = x;
    this._y = y;
  }
  get x() {
    return this._x;
  }
  get y() {
    return this._y;
  }
  // 获取向量长度
  getMagnitude() {
    return Math.sqrt(this._x ** 2 + this._y ** 2);
  }
  add(vector: Vector) {
    const v = new Vector(this._x + vector._x, this._y + vector._y);
    return v;
  }
  substract(vector: Vector) {
    const v = new Vector(this._x - vector._x, this._y - vector._y);
    return v;
  }
  dotProduct(vector: Vector) {
    return this.x * vector.x + this.y * vector.y;
  }
  edge(vector: Vector) {
    return this.substract(vector);
  }
  /** 计算与本向量正交的向量 */
  perpendicular() {
    return new Vector(this.y, -this.x);
  }
  copy() {
    return new Vector(this.x, this.y);
  }
  /** 获取归一化后的向量 */
  normalize() {
    const m = this.getMagnitude();
    return new Vector(this.x / m, this.y / m);
  }
  /** 获取法线 */
  normal() {
    const p = this.perpendicular();
    return p.normalize();
  }
}
